package com.net.xpay.common.validator;

import com.google.common.base.Strings;
import com.net.xpay.common.domain.FlexibleStaffAuthentication;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.manager.FlexibleStaffAuthenticationManager;
import com.net.xpay.common.model.ApplyFlexibleStaffAuthenticationModel;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.utils.UrlUtil;
import com.net.common.exception.BusinessException;
import com.net.common.util.AlipayAccountCheckUtil;
import com.net.common.util.SerializationUtil;
import com.net.xpay.alipay.enums.IdentityType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class FlexibleStaffAuthenticationValidator {

    @Autowired
    private FlexibleStaffAuthenticationManager flexibleStaffAuthenticationManager;

    public void validateApplyFlexibleStaffAuthentication(User user, ApplyFlexibleStaffAuthenticationModel model) {
        log.info("validateApplyFlexibleStaffAuthentication userId={}|model={}", user.getId(), SerializationUtil.obj2String(model));

        if (Strings.isNullOrEmpty(model.getIdentity())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "支付宝账号不能为空");
        }

        if (model.getIdentity().contains(" ")) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "支付宝账号不能包含空格");
        }

        if (Strings.isNullOrEmpty(model.getIdentityType())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "支付宝账号类型不能为空");
        }

        if (IdentityType.ALIPAY_LOGON_ID.getValue().equals(model.getIdentityType())) {
            if (Strings.isNullOrEmpty(model.getIdentityName())) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "支付宝账号名称不能为空");
            }

            if (!AlipayAccountCheckUtil.isEffectiveAccount(model.getIdentity())) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "支付宝账号格式不正确");
            }
        }

        if (Strings.isNullOrEmpty(model.getImportBizNo())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "外部订单号不能为空");
        }
        if (model.getImportBizNo().length() > 64) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "外部订单号长度不能超过64");
        }

        FlexibleStaffAuthentication authentication = flexibleStaffAuthenticationManager.findByImportBizNo(user.getId(), model.getImportBizNo());
        if (authentication != null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "外部订单号已被使用");
        }

        if (!Strings.isNullOrEmpty(model.getNotifyUrl()) && model.getNotifyUrl().length() > 255) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "推送地址长度不能超过255个字");
        }

        if (!Strings.isNullOrEmpty(model.getNotifyUrl())) {
            if (!UrlUtil.isHttpUrl(model.getNotifyUrl())) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "推送地址格式不正确");
            }
        }

        if (!Strings.isNullOrEmpty(model.getReturnUrl()) && model.getReturnUrl().length() > 255) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "跳转地址长度不能超过255个字");
        }
        if (!Strings.isNullOrEmpty(model.getReturnUrl())) {
            if (!UrlUtil.isHttpUrl(model.getReturnUrl())) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "跳转地址格式不正确");
            }
        }
    }

}
